package com.eascs.finance.admin.web.controller;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.finance.admin.common.controller.BaseMasterViewController;
import com.eascs.finance.admin.common.entity.ArticleEntity;
import com.eascs.finance.admin.common.vo.ArticleQueryVO;
import com.eascs.finance.admin.web.service.ArticleServiceImpl;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.context.RequestContext;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseMasterViewController {
	
	@Inject
	ArticleServiceImpl articleService;
	@Value("${web.homeUrl}")
	private String adminUrl;
	
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "propagandaManage");
	}
	
	@RequestMapping(value="/index")
	public String index(ArticleQueryVO vo){
		vo.setPageSize(10);
		PageResult<Object[]> pageResult= articleService.findArticleInfo(vo);
	  
        String[] titles = {"文章标题", "所属栏目","关键词", "发布时间",  "阅读量","是否置顶","发布人","状态"};
        AjaxConfirmButton add =new AjaxConfirmButton("  发布", "/articleRest/updateStatus?status=2&id=");
        add.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "禁用".equals(params[8])||"新建".equals(params[8]);
			}
		});
        
        AjaxConfirmButton close =new AjaxConfirmButton(" 禁止", "/articleRest/updateStatus?status=0&id=");
        close.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "发布".equals(params[8]);
			}
		});
        RedirectButton update =new RedirectButton("  编辑", "/article/edit?id=");
        AjaxConfirmButton del =new AjaxConfirmButton("  删除", "/articleRest/delete?id=");
        Button preview =ButtonHelper.createOpenButton("预览", adminUrl+"/article/showContent?articleId=");
        
        preview.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "发布".equals(params[8]);
			}
		});
		Button[] detailButton = new Button[] { add,close,update,del,preview };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setUrl(UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "showPage", ""));
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return "/article/index";
		
		
	}
	
	
	@RequestMapping(value="/add")
	public String add(){
		ArticleEntity vo=new ArticleEntity();
		vo.setIsTop(0);
		ViewContext.put("vo", vo);
		return "/article/article_edit";
	}
	
	
	@RequestMapping(value="/edit")
	public String edit(String id){
		ArticleEntity vo =articleService.findArticleById(id);
		ViewContext.put("vo", vo);
		return "/article/article_edit";
	}
	
}
